//
//  WacthTVViewController.m
//  Streaming
//
//  Created by 韩一博 on 15/6/27.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "WacthTVViewController.h"

@interface WacthTVViewController ()

@end

@implementation WacthTVViewController
- (void)dealloc
{
    [self.tableView release];
    [self.watchArray release];
    [self.headerLabel release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.watchArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
     UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    
    self.headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    self.headerLabel.font = [UIFont boldSystemFontOfSize:20];
    self.headerLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:self.headerLabel];
    self.tableView.tableHeaderView = titleView;
    [self.headerLabel release];
    
    
    [self.tableView release];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    
    self.number = 1;
    [self afnGetRequest];
    [self footerRefesh];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-unie642.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
}
- (void)leftBarButtonItemAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.watchArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}
//上拉加载
- (void)footerRefesh
{
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //        NSLog(@"%ld", self.number);
        self.isUpLoading = YES;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    }];
    
    
}


-(void)afnGetRequest
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.m.mtime.cn/TopList/TopListDetails.api?pageIndex=%ld&topListId=%ld", self.number,self.WatID];
    NSLog(@"id = %@", url_string);
    
    self.number++;
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"movies"];
        for (NSMutableDictionary *dic in array) {
            WacthTV *watchTv = [[WacthTV alloc] init];
            watchTv.WacthID = [[dic objectForKey:@"id"]integerValue];
            watchTv.title = [dic objectForKey:@"name"];
            watchTv.Entitle = [dic objectForKey: @"nameEn"];
            watchTv.number = [[dic objectForKey:@"rankNum"]integerValue];
            watchTv.posterUrl = [dic objectForKey:@"posterUrl"];
            watchTv.rating = [[dic objectForKey:@"rating"]integerValue];
            watchTv.releaseDate = [dic objectForKey:@"releaseDate"];
            watchTv.releaseLocation = [dic objectForKey:@"releaseLocation"];
            watchTv.movieType = [dic objectForKey:@"movieType"];
            watchTv.director = [dic objectForKey:@"director"];
            watchTv.actor = [dic objectForKey:@"actor"];
            watchTv.actor2 = [dic objectForKey:@"actor2"];
            watchTv.remark = [dic objectForKey:@"remark"];
            
            [self.watchArray addObject:watchTv];
 //           NSLog(@"self.watch = %@", self.watchArray);
            [watchTv release];
        }
//        [self createTable];
        
        [self.tableView reloadData];
        self.headerLabel.text = self.nameStr;
        NSLog(@"self.name = %@", self.headerLabel.text);
        [self viewWillAppear:YES];
        [self.HUD hide:YES];
        [self.tableView.footer endRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
        
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"watchCell";
    WacthTVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[WacthTVTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    WacthTV *wacthTV = [self.watchArray objectAtIndex:indexPath.row];
    cell.wacthTv = wacthTV;
//    NSLog(@"cell.wacthTv = %@", wacthTV);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
